<?php

/**
 * The followings are the available columns in table 'tbl_user':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $profile
 */
class User extends CActiveRecord
{
    public $livetime=83600;//cookie 值有效时间
    //private $livetime=time()+3600;
	/**
	 * Returns the static model of the specified AR class.
	 * @return static the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_login';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('username, password, email', 'required'),
			//array('username, password, email', 'length', 'max'=>128),
			array('userName,passWord', 'safe'),
		);
	}
    function user_login(){
        $username = Yii::app()->request->getParam('username');
        $password = Yii::app()->request->getParam('password');
        //当用户验证失败
        if(!$this->user_verify($username,$password)){ return false;}
//        if($this->livetime>0){
            setcookie('username',$username,time()+$this->livetime,'/');
            setcookie('password',$password,time()+$this->livetime,'/');
//        }else{
//            setcookie('username',$username);
//            setcookie('password',$password);
//        }

        return true;
    }
    /**
     * 验证用户是否登录
     * @param string $username 用户名
     * @param string $password 密码
     *
     * @return bool
     */
    function user_verify($username='',$password=''){
        if(empty($username) || empty($password)){
            $username = Yii::app()->request->getParam('username');
            $password = Yii::app()->request->getParam('password');
            $is_cookie = false;
        }
        if(empty($username) || empty($password)){
            $username=$_COOKIE['username'];
            $password=$_COOKIE['password'];
            $is_cookie=true;
        }
        if(empty($username) || empty($password)){
            return false;
        }

        $data=User::model()->find('userName=:username',array(':username'=>$username));

        if(empty($data->Attributes['userName'])){ return false;}
        if($data->Attributes['passWord']!=$password){ return false;}
        return true;
    }




}
